package com.jcl.core.db;


import com.jcl.core.constants.Constant;
import com.jcl.core.web.utils.RequestUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName DatabaseContext
 * @Author yyliu
 * @Date 2018/12/10 16:03
 * @Version 1.0
 **/
public class DatabaseContext {

    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();

    private static final Map<String, String> CORP_DB_NAME_MAP = new HashMap<>();

    public static void setContext(String corpCode) {
        contextHolder.set(CORP_DB_NAME_MAP.get(corpCode));
    }

    public static void setCurrentContext() {
        String corpCode = RequestUtils.getCorpCode();
        contextHolder.set(CORP_DB_NAME_MAP.get(corpCode));
    }

    public static void setServerName(String serverName) {
        contextHolder.set(serverName);
    }

    public static void setConsole() {
        contextHolder.set(Constant.DEFAULT_DB_NAME);
    }

    public static String getContext() {
        return contextHolder.get();
    }

    public static void setCorpDbNameMap(String corpCode, String dbName) {
        CORP_DB_NAME_MAP.put(corpCode, dbName);
    }

}
